package com.rcm.dda.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rcm.dda.dao.RateKeyDao;
import com.rcm.dda.exception.DDAValidationException;
import com.rcm.dda.model.RateKey;
import com.rcm.dda.validator.ErrorEnum;
import com.rcm.dda.validator.ValidatorUnit;

@Service
public class RateKeyService {

	@Autowired
	private RateKeyDao rateKeyDao;

	public List<RateKey> getAll() {
		return rateKeyDao.getAll();
	}
	
	public RateKey insert(RateKey rateKey) throws Exception {
		validateRateKey(rateKey);
		return rateKeyDao.insert(rateKey);
	}
	
	public RateKey update(RateKey rateKey) throws DDAValidationException {
		validateRateKey(rateKey);
		return rateKeyDao.update(rateKey);
	}
	
	public void delete(RateKey rateKey) throws DDAValidationException {
		validateRateKeyForDelete(rateKey);
		rateKeyDao.delete(rateKey);
	}

	public void validateRateKey(RateKey rateKey) throws DDAValidationException {
		ValidatorUnit unit = ValidatorUnit.createInstance();
		if (StringUtils.isEmpty(rateKey.getWrapKeyId())) {
			unit.add("WARP_KEY_ID", ErrorEnum.EMPTY);
		}

		if (StringUtils.isEmpty(rateKey.getWrapInsLineCd())) {
			unit.add("WARP_INS_LINE_CD", ErrorEnum.EMPTY);
		}

		if (StringUtils.isEmpty(rateKey.getWriKeyDes())) {
			unit.add("WRI_KEY_DES", ErrorEnum.EMPTY);
		}
		unit.end();
	}
	
	public void validateRateKeyForDelete(RateKey rateKey) throws DDAValidationException {
		ValidatorUnit unit = ValidatorUnit.createInstance();
		if (StringUtils.isEmpty(rateKey.getWrapKeyId())) {
			unit.add("WARP_KEY_ID", ErrorEnum.EMPTY);
		}

		if (StringUtils.isEmpty(rateKey.getWrapInsLineCd())) {
			unit.add("WARP_INS_LINE_CD", ErrorEnum.EMPTY);
		}
		
		unit.end();
	}
}
